package previous.Test_0124;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
public class Solution {
    //山脉数组的顶峰索引
    //如果a[mid]<a[mid+1]    left=mid+1;
    //如果a[mid]>=a[mid+1]  rigth=mid
    public int peakIndexInMountainArray(int[] arr) {
        int len=arr.length;
         int left=0;
         int right=len-1;
         while(left<right){
             int mid=left+(right-left)/2;
             if(arr[mid]<arr[mid+1]){
                 left=mid+1;
             }else{
                 right=mid;
             }
         }
        return left;
    }
    //寻找峰值
    public int findPeakElement(int[] arr) {
        int len=arr.length;
        int left=0;
        int right=len-1;
        while(left<right){
            int mid=left+(right-left)/2;
            if(arr[mid]<arr[mid+1]){
                left=mid+1;
            }else{
                right=mid;
            }
        }
        return left;
    }
    //寻找旋转排序数组中的最小值
    public int findMin(int[] arr) {
        int len=arr.length;
        int left=0;
        int right=len-1;
        while(left<right){
            int mid=left+(right-left)/2;
            if(arr[mid]>arr[len-1]){
                left=mid+1;
            }else{
                right=mid;
            }
        }
        return arr[left];
    }
    //点名
    public int takeAttendance1(int[] records) {
          int len=records.length-1;
          int left=0;
          int right=len-1;
          while(left<right){
              int mid=left+(right-left)/2;
              if(records[mid]==mid){
                  left=mid+1;
              }else{
                  right=mid;
              }
          }
          return  left;
    }
    public int takeAttendance(int[] records) {
        int len=records.length-1;
        int left=0;
        int right=len-1;
        while(left<right){
            int mid=left+(right-left)/2;
            if(records[mid]==mid){
                left=mid+1;
            }else{
                right=mid;
            }
        }
        if(records[left]==left){
            return left+1;
        }
        return  left;
    }
}
